import $axios from "@/network";

/**
 * {角色管理添加，编辑共用参数}
 *
 * @author: 宋康
 * @date: 2022/8/19 09:57
 */
export const rolePublic = {
  data() {
    return {
      form: {
        roleName: '',
        level: '1',
        des: '',
        meunList: []
      },
      rules: {
        roleName: [
          {required: true, message: '请输入角色名称', trigger: 'blur'},
          {min: 1, max: 100, message: '长度在 1 到 100 个字符', trigger: 'blur'}
        ],
        level: [
          {required: true, message: '请选择角色等级', trigger: 'change'}
        ],
      },
      tableData: []
    }
  },
  methods: {
    cancel() {
      this.$router.go(-1)
    },
    /**
     * {获取表格选中数据}
     * @param treeName
     * @author: 宋康
     * @date: 2022/8/19 14:37
     */
    getTreeData(treeName) {
      let checkboxData = [];
      // 全选
      let entirely = this.$refs[treeName].getCheckboxRecords();
      if (entirely.length > 0) {
        for (let p of entirely) {
          checkboxData.push(p.id);
        }
        // 半选
        let Indeterminate = this.$refs[treeName].getCheckboxIndeterminateRecords();
        if (Indeterminate.length > 0) {
          for (let p of Indeterminate) {
            checkboxData.push(p.id);
          }
        }
      }
      return checkboxData;
    }

  }
}

/**
 * {获取角色列表}
 * @param keyword
 * @param isUniversal
 * @author: 宋康
 * @date: 2022/8/19 10:12
 */
export const roleList = (keyword, isUniversal) => {
  return $axios({
    method: 'POST',
    url: '/role/query/list',
    data: {
      keyword,
      isUniversal
    }
  })
}

/**
 * {获取指定角色}
 * @param rid
 * @author: 宋康
 * @date: 2022/8/19 12:01
 */
export const queryRoleById = (id) => {
  return $axios({
    url: '/role/query/o',
    params: {
      id
    }
  })
}


/**
 * {保存角色}
 * @param option
 * @author: 宋康
 * @date: 2022/8/19 11:32
 */
export const saveRole = (option) => {
  return $axios({
    method: 'POST',
    url: '/role/save',
    data: {
      ...option
    }
  })
}

/**
 * {编辑}
 * @param option
 * @author: 宋康
 * @date: 2022/8/19 14:32
 */
export const editRole = (option) => {
  return $axios({
    method: 'POST',
    url: '/role/edit',
    data: {
      ...option
    }
  })
}

/**
 * {移除指定角色}
 * @param rid
 * @author: 宋康
 * @date: 2022/8/19 15:16
 */
export const removeRole = (rid) => {
  return $axios({
    method: 'POST',
    url: '/role/remove',
    params: {
      rid
    }
  })
}
